package main.com.oaer.action;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import main.com.oaer.model.PageList;
import main.com.oaer.model.User;
import main.com.oaer.service.UserService;
import main.com.oaer.util.CommonUtil;

/**
 *用户 控制器 
 *
 */
@Controller
@RequestMapping("/user")
public class UserAction {
	
	@Autowired
	UserService userService;	
	

    /**
     * 进入用户列表
     * @param modelMap 模型map
     * @param currentPage  当前页面数
     * @param pageSize  条数/页
     * @param user 用户对象，包含用户姓名等搜索信息
     * @return
     */
	@RequestMapping(value="/toList")
	public String entryList(ModelMap modelMap,Integer currentPage, Integer pageSize,User user){
		if(currentPage==null){
			currentPage=1;
		}
		if(pageSize==null){
			pageSize=10;
		}
		PageList roelsList = userService.findUsersByPage(currentPage, pageSize, user);
		modelMap.put("pageList", roelsList);
		return "system/listUser";
	}
	
	/**
	 * 增加一个用户，以json格式异步返回消息
	 * @param response  回应对象
	 * @param user  待插入用户对象
	 */
	@RequestMapping(value="/add")
	public void add(HttpServletResponse response, User user){
		user.setCreatetime(new Date());
		userService.add(user);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("status", "success");
		CommonUtil.printToHTML(response, map);
	}
	
	/**
	 * 删除用户
	 * @param ids  用户id连接字符串，如：1,2,3,4,5 或 简单如：9
	 */
	@RequestMapping(value="/delete")
	public void delete(HttpServletResponse response, String ids){
		Map<String,Object> map = new HashMap<String,Object>();
		if(ids!=null){
			Long[] lIds = CommonUtil.stringToLong(ids.split(","));
			for(Long lId:lIds){
				userService.delete(userService.load(lId));
			}
			map.put("status", "success");
			CommonUtil.printToHTML(response, map);
		}else{
			map.put("status", "error");
			CommonUtil.printToHTML(response, map);
		}
	}
}
